package awesome.dbase;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class DBase {
	private String DB_URL = "jdbc:mysql://199.17.162.76/AwesomeAuction";
	private String DB_DRIVER = "com.mysql.jdbc.Driver";
	private String user = "root";
	private String pswd = "";

	private static Connection conn;

	public DBase(String db_URL)
	{
		this.DB_URL = db_URL;
	}
	
	public DBase()
	{
		
	}
	
	public Connection getConnection()
	{
		return conn;
	}
	
	public void closeConnection() {
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void openConnection() {
		try {
			// Class.forName(DB_DRIVER).newInstance();
			Class.forName(DB_DRIVER)
					.newInstance();
			conn = DriverManager.getConnection(DB_URL, user, pswd);

			System.out.println("connected!");

		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void execute(String query) {
		System.out.println("Query:" + query);
		
		try {
			try {
				// Class.forName(DB_DRIVER).newInstance();
				Class.forName(DB_DRIVER)
						.newInstance();
				conn = DriverManager.getConnection(DB_URL, user, pswd);

				System.out.println("connected!");

			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			Statement s = conn.createStatement();
			s.execute (query);
			s.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public ResultSet getResultSet(String query)
	{
		ArrayList output = new ArrayList();
		this.openConnection();
		try {
			Statement st = this.conn.createStatement();
			ResultSet rs = st.executeQuery(query);
			return rs;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	public int getCount(String query) {
		System.out.println("Query:" + query);
		int count =0;
		try {
			this.openConnection();
			Statement s = conn.createStatement();
			ResultSet rs = s.executeQuery (query);
			
			rs.next();
			count = rs.getInt(1);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}
	
	public String getString(String query) {
		System.out.println("Query:" + query);
		String value ="";
		try {
			this.openConnection();
			Statement s = conn.createStatement();
			ResultSet rs = s.executeQuery (query);
			
			rs.next();
			value = rs.getString("password");
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return value;
	}
	
	public ArrayList<String> getList(String query, String attribute)
	{
		
		ArrayList<String> list = new ArrayList<String>();
		System.out.println("Query:" + query);
		String value ="";
		try {
			this.openConnection();
			Statement s = conn.createStatement();
			ResultSet rs = s.executeQuery (query);
			
			while(rs.next())
			{
				list.add(rs.getString(attribute));
				//System.out.println(rs.getString(attribute));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	public static void main(String[] args) throws SQLException {
		DBase db = new DBase();
		
		System.out.println("Count:"+db.getCount("select count(*) from user"));
	}
}
